#!/usr/bin/perl

$itemfile = 'items.csv';

open(ITEM,"$itemfile") || die "Unable to open $itemfile: $!\n";
@contents=<ITEM>;
close(ITEM);

$heading = shift(@contents);
chomp($heading);
$heading =~ s/\"//g;
@headings = split(/,/, $heading);
$size = @headings;

foreach $line (@contents) {
	chomp($line);
	$line =~ s/\"\"/0/g;
	$line =~ s/\"//g;
	@line = split(/,/, $line);
	%build = ();
	@minerals = ();
	for ($count = 0; $count < $size; $count++) {
		if ($count == 0) {
			$itemname = lc($line[$count]);
			$itemname =~ s/ //g;
			print "\[$itemname\]\n";
			$line[$count] =~ s/([\w']+)/\u\L$1/g;
			print "long_name = $line[$count]\n";
			print "description = $line[$count]\n";
			print "aoc_build_time = 6\n";
			print "research_time = 58\n";
			print "image = gfx/productionbook/book_${itemname}.png\n";
			print "coords = 149,85\n";
			$itemname =~ s/([\w']+)/\u\L$1/g;
			print "progress_list = gfx/progress/${itemname}1.png | gfx/progress/${itemname}2.png | gfx/progress/${itemname}3.png\n";
		}
		elsif ($count == 1) {
			print "tech = $line[$count]\n";
		}
		elsif ($count == 2) {
			if ($line[$count] eq 'N') { next;}
			elsif ($line[$count] eq 'A') {
				print "build = all\n";
			}
			else {
				print "build = orbital\n";
			}
		}
		elsif ($count == 3) {
			if ($line[$count] > 0) {
				push(@minerals, "Iron:${line[$count]}");
			}
		}
		elsif ($count == 4) {
			if ($line[$count] > 0) {
				push(@minerals, "Titanium:${line[$count]}");
			}
		}
		elsif ($count == 5) {
			if ($line[$count] > 0) {
				push(@minerals, "Aluminium:${line[$count]}");
			}
		}
		elsif ($count == 6) {
			if ($line[$count] > 0) {
				push(@minerals, "Carbon:${line[$count]}");
			}
		}
		elsif ($count == 7) {
			if ($line[$count] > 0) {
				push(@minerals, "Copper:${line[$count]}");
			}
		}
		elsif ($count == 8) {
			if ($line[$count] > 0) {
				push(@minerals, "Hydrogen:${line[$count]}");
			}
		}
		elsif ($count == 9) {
			if ($line[$count] > 0) {
				push(@minerals, "Deuterium:${line[$count]}");
			}
		}
		elsif ($count == 10) {
			if ($line[$count] > 0) {
				push(@minerals, "Methane:${line[$count]}");
			}
		}
		elsif ($count == 11) {
			if ($line[$count] > 0) {
				push(@minerals, "Helium:${line[$count]}");
			}
		}
		elsif ($count == 12) {
			if ($line[$count] > 0) {
				push(@minerals, "Paladium:${line[$count]}");
			}
		}
		elsif ($count == 13) {
			if ($line[$count] > 0) {
				push(@minerals, "Platinum:${line[$count]}");
			}
		}
		elsif ($count == 14) {
			if ($line[$count] > 0) {
				push(@minerals, "Silver:${line[$count]}");
			}
		}
		elsif ($count == 15) {
			if ($line[$count] > 0) {
				push(@minerals, "Gold:${line[$count]}");
			}
		}
		elsif ($count == 16) {
			if ($line[$count] > 0) {
				push(@minerals, "Silica:${line[$count]}");
			}
		}
		
		elsif ($count == 17) {
			if ($line[$count] > 0) {
				print "mass = ${line[$count]}\n";
			}
		}
	}
	$sizem = @minerals;
	$countm = 0;
	if ($sizem > 0) {
		print "materials_list = ";
		foreach $mineral (@minerals) {
			print "$mineral";
			$countm++;
			if ($countm < $sizem) {
				print " | ";
			}
			else {
				print "\n";
			}
		}
	}
	print "\n";
}
